// pages/course/course.js
import {
  getcourses,
  coursecollectadd
} from '../../api/index.js'
import {
  get,
  post
} from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    detail: [],
    tabbar: [{
      id: 0,
      text: '课程详情',
    }, {
      id: 1,
      text: '老师其他课程',
    }, {
      id: 2,
      text: '老师往期作品',
    }],
    currentTab: 0,
    markers: [{
      id: 1,
      latitude: 23.099994,
      longitude: 113.324520,
      name: 'T.I.T 创意园'
    }],
    latitude: 23.099994,
    longitude: 113.324520,
    covers: [{
      latitude: 23.099994,
      longitude: 113.344520,
      iconPath: '/image/location.png'
    }, {
      latitude: 23.099994,
      longitude: 113.304520,
      iconPath: '/image/location.png'
    }]
  },
  // 点击切换
  switchNav: function (e) {
    console.log(e);
    var that = this;
    if (this.data.currentTab == e.currentTarget.dataset.index) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.index
      });
    }
  },
  // 滑动切换
  bindChange: function (e) {
    // console.log(e)
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  async getcourse() {
    const res = await get(getcourses, {
      id: this.data.id
    })
    if (res.errCode == '10000') {
      this.setData({
        detail: res.data
      })
    }
  },
  // 收藏
  async collect() {
    let openid = wx.getStorageSync('openId')
    console.log(openid);
    const res = await post(coursecollectadd, {
      courseid: this.data.id,
      openid: openid
    })
    console.log(res);
  },
  make() {
    let token = wx.getStorageSync('token')
    // console.log(token);
    if (!token) {
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (res) => {
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          this.login()
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/orderForm/orderForm',
      })
    }

  },
  login() {
    wx.login({
      success: res => {
        // console.log(res);
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'http://www.eshareedu.cn/fkm/api/userinfo/wechatLogin',
            data: {
              code: res.code,
              appid: 'wxeb2f6528042b3bb6',
              appsecret: 'bd5b02198c30377f65596d27649f4031'
            },
            success: res => {
              console.log(res);
              if (res.data.errCode == '10000') {
                wx.setStorageSync('token', res.data.data.token)
                wx.setStorageSync('openId', res.data.data.openId)
                wx.navigateTo({
                  url: '/pages/orderForm/orderForm',
                })
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      id: options.id
    })
    this.getcourse()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})